package com.kyzh.core.activities

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.GameGoodsModel
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.requests.GameRequest
import com.gushenge.core.toast
import com.kyzh.core.R
import com.kyzh.core.dialog.TipsCenterPopupWindow
import com.kyzh.core.extension.intentExtras
import com.lxj.xpopup.XPopup
import com.tencent.qcloud.tuicore.util.ScreenUtil
import kotlinx.android.synthetic.main.act_game_background.*

// 游戏后台
class GameBackGroundActivity : BaseActivity() {

    private val gid by intentExtras("gid", "")
    // private val gid = "100002"


    companion object {
        fun start(context: Context, gid: String) {
            val intent = Intent(context, GameBackGroundActivity::class.java)
            intent.putExtra("gid", gid)
            context.startActivity(intent)
        }
    }

    // 角色
    private var role = ""

    // 大区
    private var region_id = ""

    // 物品
    private var goods_id = ""

    // 金额
    private var money = ""

    // 开通方式
    private var openingMethod: String = ""

    // 意见反馈
    private var feedBack: String = ""

    private val gameRegionList = ArrayList<GameGoodsModel>()

    private var gameMoneyList = ArrayList<GameGoodsModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_game_background)
        initView()
        initData()
    }

    private fun initView() {
        getGameInfo()
        // 搜索点击
        tvSelectedArticle.setOnClickListener {
            getGameArticle()
        }
        // 点击大区
        tvThrong.setOnClickListener {
            showGameRegionDialog()
        }
        // 点击橘色
        tvSelctRole.setOnClickListener {
            showRoleDialog()
        }
        // 发送金额
        tvArticleNameSearch.setOnClickListener {
            submitMoney()
        }
        // 发送物品
        tvSendEdArticle.setOnClickListener {
            submitArticle()
        }
        // 开通会员
        tvOpenMember.setOnClickListener {
            submitRole()
        }
        // 开通方式
        tvOpenType.setOnClickListener {
            showMsgDialog("开通方式", openingMethod)
        }
        // 意见反馈
        tvFeedback.setOnClickListener {
            showMsgDialog("意见反馈", feedBack)
        }
        // 充值金额
        edCzMoney.setOnClickListener {
            showMoneyDialog()
        }
        tvSendEdArticle.setTotalTime(6)
        tvArticleNameSearch.setTotalTime(6)
    }


    // 开通角色
    private fun submitRole() {
        val role = inputName.text.toString().trim()
        if (role.isNullOrEmpty()) {
            "请输入角色名称".toast()
            return
        }
        GameImpl.submitRole(gid, role) {

        }
    }

    private fun initData() {

    }

    // 获取游戏详情
    private fun getGameInfo() {
        GameImpl.getGameInfo(gid) {
            setTitle("${this.name}--GM后台")
            tvOpenMember.text = "点击开通${if (this.shuxing == "1") "稀有" else "普通"}后台${this.name}"
            this.region_arr?.let {
                gameRegionList.addAll(it)
            }
            openingMethod = this.kaitong_tip
            feedBack = this.problem_tip
            inputName.hint = "请输入${this.role_name}"
            this.money_detail_arr?.let {
                gameMoneyList = it
            }
        }
        tvGameGoodsSearch.setOnClickListener {
            tvSelectedArticle.text = ""
        }
    }

    // 获取游戏物品
    private fun getGameArticle() {
        GameRequest.getGameGoods(gid = gid, key = edArticleName.text.toString().trim()) {
            showArticleDialog(this)
        }
    }

    // 物品弹窗dialog
    private fun showArticleDialog(list: ArrayList<GameGoodsModel>) {
        if (list.size < 1) {
            "该物品不存在，请重新搜索".toast()
            return
        }
        val data = list.map { it.value }
        XPopup.Builder(this)
            .maxHeight(ScreenUtil.dip2px(500f))
            .asBottomList("请选择物品", data.toTypedArray()) { pos, text ->
                tvSelectedArticle.text = text
                goods_id = list[pos].id
            }
            .show()
    }

    // 获取游戏大区
    private fun showGameRegionDialog() {
        val list = gameRegionList.map { it.name }
        XPopup.Builder(this)
            .maxHeight(ScreenUtil.dip2px(500f))
            .asBottomList("请选择大区", list.toTypedArray()) { pos, text ->
                tvThrong.text = text
                region_id = gameRegionList[pos].id
            }
            .show()
    }

    // 金额充值选中弹窗
    private fun showMoneyDialog() {
        val list = gameMoneyList.map { it.name }
        XPopup.Builder(this)
            .maxHeight(ScreenUtil.dip2px(500f))
            .asBottomList("请选择充值金额", list.toTypedArray()) { pos, text ->
                edCzMoney.text = text
                money = gameMoneyList[pos].id
            }
            .show()
    }

    // 角色弹窗
    private fun showRoleDialog() {
        GameImpl.getGameRole(gid) {
            val list = this.map { it.role }
            XPopup.Builder(this@GameBackGroundActivity)
                .maxHeight(ScreenUtil.dip2px(500f))
                .asBottomList("请选择角色", list.toTypedArray()) { pos, text ->
                    tvSelctRole.text = text
                    role = this[pos].role
                }
                .show()
        }


    }

    // 弹窗
    private fun showMsgDialog(title: String, message: String) {
        XPopup.Builder(this)
            .asCustom(TipsCenterPopupWindow(this, title, message))
            .show()

    }

    // 金额充值
    private fun submitMoney() {
        if (region_id.isNullOrEmpty()) {
            "请选择大区".toast()
            return
        }
        if (role.isNullOrEmpty()) {
            "请选择角色".toast()
            return
        }
        if (money.isNullOrEmpty()) {
            "请选择充值金额".toast()
            return
        }
        GameImpl.submitMoneyCharge(
            shouyou_id = gid,
            role = role,
            money = money,
            region_id = region_id
        ) {
            if (this.code == 1) {
                tvArticleNameSearch.start()
            }
            this.msg.toast()
        }

    }

    // 物品发送
    private fun submitArticle() {
        if (region_id.isNullOrEmpty()) {
            "请选择大区".toast()
            return
        }
        if (role.isNullOrEmpty()) {
            "请选择角色".toast()
            return
        }
        if (goods_id.isNullOrEmpty()) {
            "请选择发送的物品".toast()
            return
        }
        GameImpl.submitGoodsCharge(
            shouyou_id = gid,
            role = role,
            region_id = region_id,
            goods_id = goods_id,
            goods_num = edSelectedArticleNumber.text.toString().trim()
        ) {
            if (this.code == 1) {
                tvSendEdArticle.start()
            }
            this.msg.toast()
        }

    }


}
